package com.hong.servicehosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hong.model.model.hosp.Department;
import com.hong.model.vo.hosp.DepartmentQueryVo;
import com.hong.model.vo.hosp.DepartmentVo;
import com.hong.servicehosp.repository.DepartmentRepository;
import com.hong.servicehosp.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Transactional
    @Override
    public void save(Map<String, Object> paramMap) {
        String s = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(s, Department.class);
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(Integer pNum, Integer pSize, DepartmentQueryVo departmentQueryVo) {
        Pageable pageable = PageRequest.of(pNum - 1, pSize);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase();
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }
    @Transactional
    @Override
    public void remove(String hostCode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hostCode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> getDeptListByHosCode(String hoscode) {
        List<DepartmentVo> res = new ArrayList<>();
        Department department = new Department();
        department.setHoscode(hoscode);
        //ExampleMatcher exampleMatcher=ExampleMatcher.matching().withIgnoreCase().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Example<Department> example = Example.of(department);
        List<Department> departments = departmentRepository.findAll(example);
        Map<String, DepartmentVo> bigDept = new HashMap<>(32);
        for (Department department1 : departments) {
            DepartmentVo departmentVo = bigDept.get(department1.getBigcode());
            //如果map中还没有当前科室的上级科室的话就要添加其上级科室的信息map中去
            if (departmentVo == null) {
                departmentVo = new DepartmentVo();
                departmentVo.setDepcode(department1.getBigcode());
                departmentVo.setDepname(department1.getBigname());
                departmentVo.setChildren(new ArrayList<>(50));
                bigDept.put(department1.getBigcode(), departmentVo);
            }
            //将当前节点设置为孩子节点添加进上级科室的孩子节点属性中
            DepartmentVo child = new DepartmentVo();
            child.setDepname(department1.getDepname());
            child.setDepcode(department1.getDepcode());
            departmentVo.getChildren().add(child);
        }
        for (String s : bigDept.keySet()) {
            res.add(bigDept.get(s));
        }
        return res;
    }

    @Override
    public Department getDeptName(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
